import 'package:flutter_admin/pages/common/cry/model/order_item_model.dart';
import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/models/odoo/dms_documentlocation.dart';
import 'package:flutter_admin/utils/kwargs.dart';
import 'package:flutter_admin/utils/odoo_http_util.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

class DynamicDataApi {
  // static Future<List<SelectOptionVO>> getWorkShopList() async{
  //   List<SelectOptionVO> selectList=[];
  //   try{
  //     Kwargs kwargs = new Kwargs(domain: [['active','=','1']]);
  //     var data = await OdooHttpUtil.callKw(model:'udt.dict.workshop',method:'search_read',kwargs: kwargs.toMap());
  //     for (var i = 0; i < data.length; i++) {
  //       selectList.add(SelectOptionVO(
  //           value: data[i]['id'].toString(), label: data[i]['workshop_name']));
  //     }
  //   }catch(e){
  //     selectList=[];
  //     print(e.toString());
  //   }
  //   return selectList;
  // }

  static Future<List<SelectOptionVO>> getUserSelect({val}) async {
    List<SelectOptionVO> selectList = [];
    try {
      var domain;
      if (val != null) {
        domain = [
          ["active", "=", "1"],
          ["user_id", "!=", null],
          ["department_id", "=", val]
        ];
      } else {
        domain = [
          ["active", "=", "1"],
          ["user_id", "!=", null]
        ];
      }
      Kwargs kwargs = new Kwargs(domain: domain, fields: ['user_id', 'name']);
      var data = await OdooHttpUtil.callKw(
          model: 'hr.employee', method: 'search_read', kwargs: kwargs.toMap());
      if (data.length != 0) {
        selectList.add(SelectOptionVO(value: "", label: ""));
      }
      for (var i = 0; i < data.length; i++) {
        selectList.add(SelectOptionVO(
            value: data[i]["user_id"][0].toString(), label: data[i]["name"]));
      }
    } catch (e) {
      selectList = [];
      print(e.toString());
    }
    return selectList;
  }

  static Future<List<SelectOptionVO>> getModelSelect(
      {active = true,
      required String? model,
      required String? name,
      String? tag = "id",
      String? key,
      String? operator = "=",
      val,
      String? key2,
      String? operator2 = "=",
      val2,
      OrderItemModel? order}) async {
    List<SelectOptionVO> selectList = [];
    try {
      var domain = [];
      if (active) {
        domain = [
          ["active", "=", "1"]
        ];
      }
      if (key != null) {
        domain.add([key, operator, val]);
      }
      if (key2 != null) domain.add([key2, operator2, val2]);

      Kwargs kwargs = new Kwargs(domain: domain, fields: [tag, name]);
      if (order != null) {
        kwargs.order = order.column! + " " + (order.asc! ? "asc" : "desc");
      }
      var data = await OdooHttpUtil.callKw(
          model: model, method: 'search_read', kwargs: kwargs.toMap());
      for (var i = 0; i < data.length; i++) {
        selectList.add(SelectOptionVO(
            value: data[i][tag].toString(), label: data[i][name]));
      }
    } catch (e) {
      selectList = [];
      print(e.toString());
    }
    return selectList;
  }

  static Future<ResponseBodyApi> getModelById({model, id}) async {
    ResponseBodyApi responseBodyApi =
        await getModels(model: model, key: 'id', val: id);
    if (responseBodyApi.success!) {
      if (responseBodyApi.data.length == 0) {
        responseBodyApi = ResponseBodyApi(success: false, message: "该数据已丢失");
      } else {
        responseBodyApi =
            ResponseBodyApi(success: true, data: responseBodyApi.data[0]);
      }
    }
    return responseBodyApi;
  }

  static Future<ResponseBodyApi> getModels(
      {required model,
      key,
      String? operator = "=",
      val,
      key2,
      String? operator2 = "=",
      val2}) async {
    ResponseBodyApi responseBodyApi;
    try {
      var domain = [];
      if (key != null) {
        domain.add([key, operator, val]);
      }
      if (key2 != null) domain.add([key2, operator2, val2]);
      Kwargs kwargs = new Kwargs(domain: domain);

      var data = await OdooHttpUtil.callKw(
          model: model, method: 'search_read', kwargs: kwargs.toMap());
      responseBodyApi = ResponseBodyApi(success: true, data: data);
    } catch (e) {
      print(e.toString());
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  // static Future<List<SelectOptionVO>> getUserList({workshopId}) async{
  //   List<SelectOptionVO> selectList=[];
  //   try{
  //     Kwargs kwargs;
  //     if(workshopId!=null){
  //       kwargs = new Kwargs(domain: [['active','=','1'],['workshop_id','=',int.parse(workshopId)]]);
  //     }else{
  //       kwargs = new Kwargs(domain: [['active','=','1']]);
  //     }
  //     var data = await OdooHttpUtil.callKw(model:'user',method:'search_read',kwargs: kwargs.toMap());
  //     for (var i = 0; i < data.length; i++) {
  //       selectList.add(SelectOptionVO(
  //           value: data[i]['id'].toString(), label: data[i]['user_name']));
  //     }
  //   }catch(e){
  //     selectList=[];
  //     print(e.toString());
  //   }
  //   return selectList;
  // }

  static Future<ResponseBodyApi> dictionaryList(code) async {
    ResponseBodyApi responseBodyApi;
    try {
      Kwargs typeKwargs = new Kwargs(domain: [
        ['typecode', '=', code],
        ['active', '=', '1']
      ]);
      var codeData = await OdooHttpUtil.callKw(
          model: 'base.dictionarytype',
          method: 'search_read',
          kwargs: typeKwargs.toMap());
      Kwargs dataKwargs = new Kwargs(domain: [
        ['type_id', '=', codeData[0]['id']],
        ['active', '=', '1']
      ]);
      var data = await OdooHttpUtil.callKw(
          model: 'base.dictionarydata',
          method: 'search_read',
          kwargs: dataKwargs.toMap());
      responseBodyApi = ResponseBodyApi(success: true, data: data);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<List<SelectOptionVO>> getDictList(typeCode) async {
    var res = await dictionaryList(typeCode);
    print(res);
    List<SelectOptionVO> selectList = [];
    if (res.success!) {
      List<dynamic> dynamicList = res.data;
      if (dynamicList.length != 0) {
        selectList.add(SelectOptionVO(value: "", label: ""));
      }
      for (var i = 0; i < dynamicList.length; i++) {
        selectList.add(SelectOptionVO(
            value: dynamicList[i]['id'].toString(),
            label: dynamicList[i]['dictionaryname']));
      }
    }
    return selectList;
  }

  static Future<ResponseBodyApi> devicePrint({required data}) async {
    ResponseBodyApi responseBodyApi;
    try {
      var map = {
        "name": "打印机任务",
        'paper_id': 1,
        'template_id': 1,
        'site_id': 1,
        'queue_list': data
      };
      var result = await OdooHttpUtil.callKw(
          model: 'print_service.print_queue', method: 'create', args: [map]);
      responseBodyApi = ResponseBodyApi(success: true, data: result);
    } catch (e) {
      responseBodyApi =
          ResponseBodyApi(success: false, message: '请求出错了：' + e.toString());
    }
    return responseBodyApi;
  }

  static Future<List<SelectOptionVO>> getUserListByDeptID({data}) async {
    List<SelectOptionVO> selectList = [];
    List domainList = data['domainList'];
    Kwargs recordsKwargs = Kwargs(domain: domainList);
    var result = await OdooHttpUtil.callKw(
        model: 'purchase.evaluation.project',
        method: 'search_user_project',
        kwargs: recordsKwargs.toMap());
    for (var i = 0; i < result.length; i++) {
      // Fluttertoast.showToast(
      //     msg:
      //         '拿到的两个值，第一个值：${result[i][0]} 是否为String 类型：${result[i][0].toString() is String} , 第二个值为：${result[i][1]}');
      selectList.add(SelectOptionVO(
          value: (result[i][0]).toString(), label: result[i][1]));
    }
    return selectList;
  }
}
